<?php
class Subscription_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }
    
    function get($data)
    {
    	$this->db->select('job.*, company.name AS company, candidate.idmember,
    	subscription.idcandidate, candidate.firstname, candidate.lastname,
    	candidate_thumbnail.thumbnail, subscription.idstatus, subscription.id AS idsubscription');
    	$this->db->from('subscription');
    	$this->db->join('job', 'subscription.idjob = job.id', 'left');
    	$this->db->join('company', 'job.idcompany = company.id', 'left');
    	$this->db->join('candidate', 'subscription.idcandidate = candidate.id', 'left');
    	$this->db->join('candidate_thumbnail', 'candidate_thumbnail.idmember = candidate.idmember AND candidate_thumbnail.idnow = \'Y\'', 'left');
    	
    	if (isset($data['idsubscription']) && is_numeric($data['idsubscription'])) {
    		$this->db->where('subscription.id', $data['idsubscription']);
    	}
    	
    	if (isset($data['idjob']) && is_numeric($data['idjob'])) {
    		$this->db->where('subscription.idjob', $data['idjob']);
    	}
    	
    	if (isset($data['idstatus']) && is_array($data['idstatus'])) {
    		$this->db->where_in('subscription.idstatus', $data['idstatus']);
    	}
    	
   	 	if (isset($data['idcandidate']) && is_numeric($data['idcandidate'])) {
    		$this->db->where('subscription.idcandidate', $data['idcandidate']);
    	}
    	
    	return $this->db->get();
    }
    
	function insert($data)
    {
    	$this->db->trans_begin();
    	
    	$data_insert = array(
		   'idjob' => $data['idjob'],
		   'idcandidate' => $data['idcandidate'],
		   'idstatus' => $data['idstatus']
		);
		
		$this->db->set('date', 'NOW()', false);
		$this->db->insert('subscription', $data_insert);
		
		$this->db->set('idmemberfrom', $this->session->userdata['idmember']);
		$this->db->set('idmemberfromtype', 1);
		$this->db->set('idmemberto', '(select idmember from job inner join company on company.id = job.idcompany where job.id = '.$data['idjob'].')', false);
		$this->db->set('idmembertotype', 2);
		$this->db->set('idjob', $data['idjob']);
		$this->db->set('idtype', 1);
		$this->db->set('message', '');
		$this->db->set('date', 'NOW()', false);
		$this->db->insert('message');
		
    	if ($this->db->trans_status() === false)
		{
		    $this->db->trans_rollback();
		    return false;
		}
		else
		{
		    $this->db->trans_commit();
		    return true;
		}
    }
    
	function delete($data)
    {
    	$this->db->trans_begin();
		
		$this->db->delete('subscription', array('idjob' => $data['id'], 'idcandidate' => $data['idcandidate']));
		
    	if ($this->db->trans_status() === false)
		{
		    $this->db->trans_rollback();
		    return false;
		}
		else
		{
		    $this->db->trans_commit();
		    return true;
		}
    }
    
    function check_view_profile($subscription_id = null)
    {
    	$this->db->set('idstatus', 2);
    	$this->db->where('id', $subscription_id);
    	$this->db->where('idstatus', 1);
		$this->db->update('subscription');
    }
    
	function check_assessment($subscription_id = null)
    {
    	$this->db->set('idstatus', 3);
    	$this->db->where('id', $subscription_id);
    	$this->db->where_in('idstatus', array(1,2));
		$this->db->update('subscription');
    }
    
	function check_finalist($subscription_id = null)
    {
    	$this->db->set('idstatus', 4);
    	$this->db->where('id', $subscription_id);
    	$this->db->where('idstatus', 3);
		$this->db->update('subscription');
    }
    
	function check_engaged($subscription_id = null)
    {
    	$this->db->set('idstatus', 6);
    	$this->db->where('id', $subscription_id);
    	$this->db->where('idstatus', 4);
		$this->db->update('subscription');
    }
    
	function check_discarded($subscription_id = null)
    {
    	$this->db->set('idstatus', 5);
    	$this->db->where('id', $subscription_id);
		$this->db->update('subscription');
    }
}
?>